unit FireWallXp;

interface

uses
  ActiveX, ComObj, Variants;

Type
  TFireWallXP = Class
    private
    public
      constructor Create;
      procedure Add(sName:String; port:integer);
      procedure Remove(sName:String; port:integer);

  End;  

implementation

const
  NET_FW_PROFILE_DOMAIN      =  0;
  NET_FW_PROFILE_STANDARD    =  1;
  NET_FW_IP_PROTOCOL_TCP     = 6;
  NET_FW_IP_PROTOCOL_UDP     = 17;
  NET_FW_SCOPE_ALL           =  0;
  NET_FW_IP_VERSION_ANY      =  2;

{ TFireWallXP }

procedure TFireWallXP.Add(sName: String; port: integer);
var
  ovMgr:         OleVariant;
  ovProfile:     OleVariant;
  ovPort:        OleVariant;
begin
  ovMgr:=CreateOleObject('HNetCfg.FwMgr');
  try
     ovProfile:=ovMgr.LocalPolicy.CurrentProfile;
     try
        ovPort:=CreateOleObject('HNetCfg.FwOpenPort');
        try
           ovPort.Port:=Port;
           ovPort.Name:=sname;
           ovPort.Scope:=NET_FW_SCOPE_ALL;
           ovPort.IpVersion:=NET_FW_IP_VERSION_ANY;
           ovPort.Protocol:=NET_FW_IP_PROTOCOL_TCP;
           ovPort.Enabled:=True;
           ovProfile.GloballyOpenPorts.Add(ovPort);
        finally
           ovPort:=Unassigned;
        end;
     finally
        ovProfile:=Unassigned;
     end;
  finally
     ovMgr:=Unassigned;
  end;
end;

constructor TFireWallXP.Create;
begin
  CoInitialize(nil);
end;

procedure TFireWallXP.Remove(sName: String; port: integer);
var
  ovMgr:         OleVariant;
  ovProfile:     OleVariant;
  ovPort:        OleVariant;
begin
  ovMgr:=CreateOleObject('HNetCfg.FwMgr');
  try
     ovProfile:=ovMgr.LocalPolicy.CurrentProfile;
     try
        ovPort:=CreateOleObject('HNetCfg.FwOpenPort');
        try
           ovPort.Port:=Port;
           ovPort.Name:=sname;
           ovPort.Scope:=NET_FW_SCOPE_ALL;
           ovPort.IpVersion:=NET_FW_IP_VERSION_ANY;
           ovPort.Protocol:=NET_FW_IP_PROTOCOL_TCP;
           ovPort.Enabled:=True;
           ovProfile.GloballyOpenPorts.Remove(port, NET_FW_IP_PROTOCOL_TCP);
        finally
           ovPort:=Unassigned;
        end;
     finally
        ovProfile:=Unassigned;
     end;
  finally
     ovMgr:=Unassigned;
  end;
end;

end.
